<?php
namespace app\admin\controller;
use think\Db;
use think\Controller;

class Login extends Controller
{
    public function login()
    {	
    	$info = session("info");
    	if($info){
    		$this->redirect("Index/index");
    	}
        return $this->fetch();
    }

    public function do_login()
    {
    	$account = input("post.account");
		$passwd  = input("post.passwd");
		if($account==""){
			$this->error("账号不能为空");
		}
		if($passwd==""){
			$this->error("密码不能为空");
		}
		$data = db('admin')->where(['account'=>$account])->find();
		
		if(!$data){
			$this->error("账号或者密码错误！");
		}elseif($data['staus']==0){
			$this->error("该账号被禁止使用!");
		}else{
			if($data['password']!=md5($passwd)){
				$this->error("账号或者密码错误！");
			}else{
				$condition['a.id']=$data['id'];
				$data['head_image'] = IMG_PATH.$data['head_image'];
				session("info",$data);
				$l['ip']   = get_client_ip();
				$l['logintime'] = time();
				db('admin')->where(['account'=>$account])->update($l);
				$this->success('登录成功','Index/Index');
			}
		}
    }

    public function up_login(){
    	session("info",null);
    }
}
